/*
 * @Description  : 
 * @Autor        : yanwang
 * @CreateDate   : 2021-11-10 11:56:51
 * @LastEditors  : yanwang
 * @LastEditTime : 2021-11-10 14:41:59
 */

/*
 * @lc app=leetcode.cn id=241 lang=javascript
 *
 * [241] 为运算表达式设计优先级
 */

// @lc code=start
/**
 * @param {string} expression
 * @return {number[]}
 */

/**
 * 
 * @param {*} expression 
 */
/**
 * 1. 采用分治
 * 2. 遍历 字符串
 * 3. 遇到操作符，就将左右两边的字符串，分别当作两个表达式
 */
// 返回所有计算结果的集合
var diffWaysToCompute = function(expression) {
  let ret = [];
  for (let i = 0; i < expression.length; i++) {
    const str = expression[i];
    if(str !== '+' && str !== '-' && str !=='*') continue;

    const left = expression.slice(0, i);
    const right = expression.slice(i + 1, expression.length);

    const valXs = diffWaysToCompute(left);
    const valYs = diffWaysToCompute(right);

    for (let i = 0; i < valXs.length; i++) {
      for (let j = 0; j < valYs.length; j++) {
        switch(str) {
          case '+': 
            ret.push(valXs[i] + valYs[j]);
            break;
          case '-': 
            ret.push(valXs[i] - valYs[j]);
            break;
          case '*':
            ret.push(valXs[i] * valYs[j]); 
            break;
        }
      }
    }
  }
  // 如果expresion是数字字符串
  if(ret.length === 0 ) {
    let val = 0;
    for (let i = 0; i < expression.length; i++) {
      val = val * 10 + +expression[i];
    }
    ret.push(val);
  }

  return ret;
};
// @lc code=end